# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.paymentDao import PaymentAPI
import time
from . import app
from com.zzh.dao_v2.AuthTokenOperation import authTokenOperation
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
CORS(app, supports_credentials=True)


# ------------------ payment 表API ---------------
@app.route('/addPayment', methods=['POST'])
def addPaymentNoToken():
    if not request.json:
        abort(400)
    payment_info = {
        'paymentName': request.json.get('paymentName', ),
        'paymentImgUrl': request.json.get('paymentImgUrl', ),
        'paymentRemark': request.json.get('payment_remark', ),
        'paymentType': request.json.get('paymentType', 1),
        'paymentPoundageType': request.json.get('paymentPoundageType', 1),
        'paymentPoundageAmount': request.json.get('paymentPoundageAmount', ),
        'paymentSort': request.json.get('paymentSort', 99),
        'paymentIsLock': request.json.get('paymentIsLock', 0),
        'paymentApiPath': request.json.get('paymentApiPath', ),
    }
    try:
        rs = PaymentAPI().addPayment(payment_info)
        if rs:
            return jsonify({  "message": {"code": 1, "info": "添加成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "添加失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/updatePayment', methods=['POST'])
def updatePaymentNoToken():

    try:
        id = request.json.get('id')
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = PaymentAPI().selectPayment(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    updatePayment = {
        'paymentName': request.json.get('paymentName',oldInfo[1] ),
        'paymentImgUrl': request.json.get('paymentImgUrl', oldInfo[2] ),
        'paymentRemark': request.json.get('payment_remark', oldInfo[3] ),
        'paymentType': request.json.get('paymentType',oldInfo[4]  ),
        'paymentPoundageType': request.json.get('paymentPoundageType',oldInfo[5]  ),
        'paymentPoundageAmount': request.json.get('paymentPoundageAmount', oldInfo[6] ),
        'paymentSort': request.json.get('paymentSort',oldInfo[7]  ),
        'paymentIsLock': request.json.get('paymentIsLock', oldInfo[8] ),
        'paymentApiPath': request.json.get('paymentApiPath',oldInfo[9]  ),
    }
    if not updatePayment:
        return jsonify({"message": {"code": 0, "info": "参数传递不完整"}})
    try:
        updatePaymentResult = PaymentAPI().updatePaymentById(updatePayment, id)
        if updatePaymentResult:
            return jsonify({"info": updatePayment,
                            "massage": {"code": 1, "info": "支付类型表修改成功"}})
        else:
            return jsonify({"message": {"code": 0, "info": "支付类型表修改失败"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryPaymentById', methods=['POST'])
def queryPaymentByIdNoToken():
    id = request.json.get('id')
    try:
        queryResult, PaymentListJson = PaymentAPI().queryPaymentById(id)
        if queryResult:
            return jsonify({"info": PaymentListJson, "totalCount": queryResult,
                            "message": {"code": 1, "info": "支付类型查询成功！"}})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/queryPaymentByCondition', methods=['POST'])
def queryPaymentByConditionNoToken():

        if not request.json:
            abort(400)


        try:
            page = request.json['page']
            pageIndex = page["pageIndex"]
            pageSize = page["pageSize"]
        except:
            return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
        try:
            conditions = request.json['condition']
        except:
            return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
        try:
            multiSort = request.json['multiSort']
        except:
            return jsonify({"message": "未传入排序规则multiSort"})
        try:
            total_count, UserCodeJSON = PaymentAPI().queryPaymentByCondition(pageIndex=pageIndex, pageSize=pageSize,
                                                                             conditions=conditions, multiSort=multiSort)
            if UserCodeJSON:
                return jsonify(
                    {"message": {"code": 1, "status": "queryPayment OK"}, "total": total_count, "info": UserCodeJSON})
            else:

                return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
        except Exception, e:
                return jsonify({"message": {"code": 0, "info": str(e)}})







@app.route('/deletePayment', methods=['POST'])
def deletePaymentNoToken():
    paymentId =request.json.get('ids')
    try:
        deletePaymentResult = PaymentAPI().deletePaymentById(paymentId)
        if deletePaymentResult:
            return jsonify({"message": {"code": 1, "info": "删除支付记录成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "删除支付记录失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

